<?php
/**
*每个店的统计数据
*/

//地址栏传来的地址,根据此地址

date_default_timezone_set('Asia/Shanghai');
//初始化数据
require('../include/init.php');
//aa
//连接数据库
$mysql=mysql::getIns();
// $data=$mysql->getAll("select * from alldev");
// print_r($data);
// print_r($data);
//统计当前在线人数    (在线人数为当前时间的十分钟之前到现在的人数)

/**
*从区域表中读出来有多少区域，然后根据这些区域分别跑一边
**/
$sql="select areaname from area";
$area=$mysql->getAll($sql);
foreach($area as $v){
  $address=$v['areaname'];
  $maxtime=time();
  $mintime=$maxtime-10*60;
  // echo $maxtime . '<br />';
  // echo $mintime;
  $sql="select count(*) from alldev where timestamp > $mintime and timestamp < $maxtime and whitelist <> '1' and shop='". $address ."'";
  $allnow=$mysql->getOne($sql);
  // echo $sql;
  // echo $allnow;
  // exit;
  // print_r($allnow);
  // exit;


  //统计当天累计人数数
  $t = time();
  $start = mktime(0,0,0,date("m",$t),date("d",$t),date("Y",$t));
  $end = mktime(23,59,59,date("m",$t),date("d",$t),date("Y",$t));
  $sql="select count(*) from alldev where timestamp > $start and timestamp < $end and whitelist <> '1' and shop='" . $address . "'";
  $allday=$mysql->getOne($sql);
  // echo $allday;
  // exit;
  //首次今天首次进店人数,通过字段visittimes为1来进行判断
  $sql="select count(*) from alldev where visittimes=1 and timestamp > $mintime and timestamp < $maxtime and whitelist <> '1' and shop='".$address ."'";
  $first=$mysql->getOne($sql);

  //统计当前销售区
  $sql="select count(*) from alldev where timestamp > $mintime and timestamp < $maxtime and whitelist <> '1' and shop='" . $address ."' and area in ('展厅区','销售区','精品区')";
  $sale=$mysql->getOne($sql);
  // echo $sale;
  // print_r($sale);
  // exit;

  //统计当前售后区
  $service=$allnow-$sale;


  //今天多次进店人数
  $sql="select count(*) from alldev where visittimes <> 1 and timestamp > $mintime and timestamp < $maxtime and whitelist <> '1' and shop='" . $address ."'";
  $second=$mysql->getOne($sql);

  //统计登陆人数
  $sql="select count(*) from alldev where isOL=1 and timestamp > $mintime and timestamp < $maxtime and whitelist <> '1' and shop='" . $address ."'";
  $login=$mysql->getOne($sql);
  // echo $login;

  //统计未登陆人数
  $sql="select count(*) from alldev where isOL=2 and timestamp > $mintime and timestamp < $maxtime and whitelist <> '1' and shop='" . $address ."'";
  $nolog=$mysql->getOne($sql);
  // echo $nolog;

  //统计历史进店人数
  $sql="select count(*) from alldev where whitelist <> '1' and shop='". $address ."'";
  $history=$mysql->getOne($sql);

  /**********************************************************


                        写入redis


  ********************************************************/


switch ($address) {
  case '北京博瑞祥星汽车销售有限公司':
    $hash='4S_GManager_brxx';
    break;
  case '义乌市博奥汽车销售服务有限公司':
    $hash='4S_GManager_ywba';
    break;
  case '广东粤奥汽车销售服务有限公司':
    $hash='4S_GManager_gdya';
    break;
  case '江苏天奥汽车销售服务有限公司':
    $hash='4S_GManager_jsta';
    break;
  case '辽宁鑫迪汽车销售服务有限公司':
    $hash='4S_GManager_lnxd';
    break;
  case '成都新元素雅麓汽车销售服务有限公司':
    $hash='4S_GManager_cdyl';
    break;
  case '云南驰泰汽车销售服务有限公司':
    $hash='4S_GManager_ynct';
    break;
  case '宁夏奥立升汽车销售服务有限公司':
    $hash='4S_GManager_nxals';
    break;
  case '陕西奥诚汽车销售服务有限公司':
    $hash='4S_GManager_sxac';
    break;
}
$redis = new Redis();
  //连接服务器
  $redis->connect("localhost");
  //授权
  $redis->auth("redis=pwd@2016");

$redis->hmset($hash,array('allnow'=>$allnow,
'allday'=>$allday,
'first'=>$first,
'second'=>$second,
'sale'=>$sale,
'service'=>$service,
'login'=>$login,
'nolog'=>$nolog,
'history'=>$history));
}
